/*
 * Group:PrePool K9-357 
 * 8/2010
 */
package control;

import java.io.IOException;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletSession;

import org.apache.log4j.Logger;

import form.BaseForm;
import form.ManageHome;

import blo.DictionariesBLO;
import blo.ManagerindictBLO;

/**
 * Control class for S01_LookUpWord.jsp Methods is called in method
 * processAction(ActionRequest request, ActionResponse response) of
 * OnlineDictPortlet class
 */
public class SearchWord extends BaseControl {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	Logger LOG = Logger.getLogger("SearchWord");

	/**
	 * Perform when click translate button
	 * 
	 * @param request
	 * @param response
	 * @return return the form that include meaning contain in txtMeaning
	 * @throws PortletException
	 * @throws IOException
	 */
	public form.SearchWord procTranslate(ActionRequest request,
			ActionResponse response) throws PortletException, IOException {
		int slFromId = Integer.valueOf(request.getParameter("slFrom"));
		int slToId = Integer.valueOf(request.getParameter("slTo"));
		request.setAttribute("from", new Integer(slFromId));
		request.setAttribute("to", new Integer(slToId));

		String txtWord = request.getParameter("txtWord");
		DictionariesBLO dictBLO = new DictionariesBLO();
		String meaning = dictBLO.LookupWord(slFromId, slToId, txtWord);
		form.SearchWord result = new form.SearchWord();
		result.setSlFrom(slFromId);
		result.setSlTo(slToId);
		result.setTxtmeaning(meaning);
		result.setTxtWord(txtWord);
		LOG.info("meaning " + meaning);
		return result;
	}

	/**
	 * Perform when click Login button
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws PortletException
	 * @throws IOException
	 */
	public form.LoginForm procLogin(ActionRequest request,
			ActionResponse response) throws PortletException, IOException {
		return null;
	}

	/**
	 * Perform when click Contribute button
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws PortletException
	 * @throws IOException
	 */
	public form.Contribute procContribute(ActionRequest request,
			ActionResponse response) throws PortletException, IOException {
		request.getPortletSession().setAttribute("message", "",
				PortletSession.APPLICATION_SCOPE);
		return null;
	}

	/**
	 * Perform when click Logout button
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws PortletException
	 * @throws IOException
	 */
	public BaseForm procLogout(ActionRequest request, ActionResponse response)
			throws PortletException, IOException {
		request.getPortletSession().setAttribute("username", "Guest",
				PortletSession.APPLICATION_SCOPE);
		request.getPortletSession().setAttribute("userrole", -1,
				PortletSession.APPLICATION_SCOPE);
		return null;
	}
	
	public ManageHome procManagerHome(ActionRequest request, ActionResponse response)
			throws PortletException, IOException {
		String username = (String) request.getPortletSession().getAttribute("username", PortletSession.APPLICATION_SCOPE);
		LOG.info(username);
		int userrole = (Integer) request.getPortletSession().getAttribute("userrole", PortletSession.APPLICATION_SCOPE);
		blo.UsersBLO loginUser = new blo.UsersBLO();
		blo.ManagerindictBLO manageBLO = new ManagerindictBLO();
		ManageHome result = new ManageHome();
		
		result.setApproverManager(manageBLO.getApproverManagement(username));
		result.setReviewerManager(manageBLO.getReviewerManagement(username));
		result.setLoginUsername(username);
		result.setRole(userrole);
		return result;
	
	}
	
}
